<template>
	<view>
		<view class="content_box">
			<view @click="getPerson" class="getPerson">
				选择就诊人<view class="right">{{medicalData.xming0}}<view class="icon right"></view></view>
			</view>
			<view class="content_box_list">
				<h3>预约核酸项目</h3>
				<radio-group @change="radioChange">
				<ul>
					<li v-for="(item,index) in subjectList" :key="index">
						<label>
							<radio v-if="index==0" :value="''+index"  name="box" color="#1EB21B" checked="true"></radio>
							<radio v-else :value="index"  name="box" color="#1EB21B"></radio>
							{{item.xmmc00}}
						</label>
					</li>
				</ul>
				</radio-group>
			</view>
		</view>
		<view class="money"><h3>金额 <span class="right">{{money}}元</span></h3></view>
		<button class="sure_btn" @click="sureAppointment">确认预约</button>
		<personList styles="display:none;" ref="personList" v-on:checkedPerson= "getDatas"></personList>
	</view>
</template>

<script>
	import personList from "../../modules/personList.vue";
	import pay from "@/tool/pay.js";
	export default {
		data() {
			return {
				out_trade_no:"",
				medicalData:{"name":""},
				subjectList:[],
				checkedSubject:{},
				money:0.00
			}
		},
		components:	{
			personList
		},
		onLoad() {
			this.init();
		},
		methods: {
			init(){
				let params={
					"xmid00":"1",
					"xmlx00":"1",
					"funid":"F07.20.02.01"
				};
				uni.showLoading({
					mask:true,
					title:"项目加载中...",
				})
				let user=uni.getStorageSync("user");
				this.$postJson("/qnyesb/qnyesbproxy",params,res=>{
					if(res.state==0){
						let data=res.data.retrieve;
						this.subjectList=data;
						//初始化数据检索
						if(data.length>0){
							let checkedSubject=data[0];
							this.checkedSubject=checkedSubject;
							this.money=checkedSubject.xmdj00;
							this.medicalData=user; 
						}else{
							uni.showToast({
								title:"暂无核酸检测项目",
								icon:"none",
								duration:3000
							})
						}
					}else{
						uni.showToast({
							title:res.message,
							icon:"none",
							duration:3000
						})
					}
					uni.hideLoading();
				})
				
			},
			sureAppointment(){
				let date=new Date();
				this.out_trade_no="WXNAT"+date.getTime();
				let checkedSubject=this.checkedSubject;
				let yeaer=date.getFullYear();
				let month=date.getMonth()+1;
				if(month<10){
					month="0"+month;
				}
				let day=date.getDate();
				if(day<10){
					day="0"+day;
				}
				let yysj00=yeaer+"-"+month+"-"+day;
				let params={
					"kh0000":this.medicalData.kh0000,
					"klx000":"3",
					"patientId":this.medicalData.patid0,
					"phone":this.medicalData.lxdh00,
					"xming0":this.medicalData.xming0,
					"xmid00":this.checkedSubject.xmid00,
					"xmbm00":this.checkedSubject.xmbm00,
					"xmlx00":"1",
					"xmsl00":"1",
					"zdksid":"",//开单科室ID
					"zxksid":"",//执行科室ID
					"yysj00":yysj00,//预约时间
					"ddje00":this.checkedSubject.xmdj00,//订单金额
					"funid":"F07.20.02.02",
				}
				uni.showLoading({
					title:"加载中...",
					mask:true
				})
				this.$postJson("/qnyesb/qnyesbproxy",params,res=>{
					if(res.state==0){
						this.payW(res.data.retrieve[0]);
					}else{
						uni.showToast({
							title:res.message,
							icon:"none",
							duration:3000
						})
						uni.hideLoading();
					}
				})
				
			},
			payW(res){
				//支付，结算
				var payJson={
					"out_trade_no": this.out_trade_no,//后台生成，唯一性
					"description": "核酸检测",
					"notify_url": "http://127.0.0.1",//异步回调地址，后台配置
					"amount": {
						"total": (parseFloat(this.checkedSubject.xmdj00) *100),//(parseFloat(this.params.ghzlf0) *100
						"currency": "CNY"
					},
					"payer": {
						"openid": uni.getStorageSync("openid")
					}
				};
				pay.prePay(payJson,success=>{
					this.sureAppointmentOver(res);
				},error=>{
					//取消支付，支付失败等等 取消或更新失败状态
					uni.showToast({
						title:"支付失败",
						icon:"error"
					})
					uni.hideLoading();
				})
			},
			sureAppointmentOver(result){
				let params={
					kh0000:this.medicalData.kh0000,
					klx000:"3",
					xming0:this.medicalData.xming0,
					xmid00:this.checkedSubject.xmid00,
					djh000:result.jfdjh0,
					djje00:this.checkedSubject.xmdj00,
					jylsh0:""+new Date().getTime(),
					ddh000:this.out_trade_no,
					bcfyze:this.checkedSubject.xmdj00,
					zffs00:"2",
					funid:"F07.20.02.03"
				}
				this.$postJson("/qnyesb/qnyesbproxy",params,res=>{
					if(res.state==0){
						uni.showToast({
							title:"支付成功",
							duration:3000
						})
					}else{
						uni.showModal({
							title:"温馨提示",
							content:res.message+",点击确定返回首页",
							success(res) {
								if(res.confirm){
									uni.switchTab({
										url:"../index/index"
									})
								}
							}
						})
					}
					uni.hideLoading();
				})
			},
			radioChange(e){
				var index=e.detail.value;
				var checkedSubject=this.subjectList[index];
				this.checkedSubject=checkedSubject;
				this.money=checkedSubject.xmdj00;
			},
			getPerson(){
				this.$refs.personList.selectPerson();
			},
			getDatas(item){
				this.medicalData=item;
			}
		}
	}
</script>

<style>
	.content_box{
		width: 100%;
		background-color: #FFFFFF;
		margin-top: 10px;
	}
	.content_box .getPerson{
		padding: 15px;
		border-bottom: 1px solid #d9d2d2;
	}
	.content_box .getPerson .icon{
		width: 12.29px;
		height: 11.88px;
		background-image: url(../../static/124@2x.png);
		background-size: 100% 100%;
		margin-top: 5px;
	}
	.content_box .content_box_list{
		padding: 15px;
		display: inline-block;
		padding-bottom: 30px;
	}
	.content_box .content_box_list ul li{
		font-size: 16px;
		display: flex;
		align-items: center;
		padding-top: 20px;
		
	}
	.money{
		width: 96%;
		margin: 0 auto;
		padding-top: 30px;
		padding-bottom: 30px;
	}
	.money .right{
		color: #1EB21B;
	}
	.sure_btn{
		color: #FFFFFF;
		background-color: #1EB21B;
		border-radius: 28px;
	}
</style>
